package com.gxsyh.nantie.phone.consumer.controller;

import com.google.common.cache.Cache;
import com.gxsyh.nantie.commons.dto.ResponseResult;
import com.gxsyh.nantie.phone.consumer.controller.dto.UpdateImagesParam;
import com.gxsyh.nantie.phone.provider.api.TbHomeImgService;
import com.gxsyh.nantie.phone.provider.api.domain.TbHomeImg;
import org.apache.dubbo.config.annotation.Reference;
import org.redisson.api.RBucket;
import org.redisson.api.RedissonClient;
import org.redisson.codec.KryoCodec;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @email yom535@outlook.com
 * @author: 刘锋嘉(Liu Fengjia)
 * @date: 2020/1/20
 * @time: 09:55
 * @Description:
 */
@RestController
public class PhoneHomeImageController {

    @Reference(version = "1.0.0")
    TbHomeImgService tbHomeImgService;

    @Resource
    RedissonClient redissonClient;

    @Resource(name = "CacheCon")
    Cache cache;

    @PostMapping(value = "/phone/update/images")
    public ResponseResult<Void> updateImages(@RequestBody UpdateImagesParam updateImagesParam, HttpServletResponse response){



        RBucket<List<TbHomeImg>> homeImage = redissonClient.getBucket("homeImage",new KryoCodec());
        int result = tbHomeImgService.updateImage(updateImagesParam.getId(), updateImagesParam.getImage());
        if (result>0){
            homeImage.delete();
            return new ResponseResult<>(HttpStatus.OK.value(),"上传图片成功",null);
        }
        else
        {
            response.setStatus(HttpStatus.BAD_REQUEST.value());
            return new ResponseResult<Void>(HttpStatus.BAD_REQUEST.value(),"图片上传失败",null);
        }
    }

    @GetMapping(value = "/phone/get/all/home/images")
    public ResponseResult<List<TbHomeImg>> getAllHomeImages(){
        //本地缓存
        List<TbHomeImg> homeImageCache = (List<TbHomeImg>) cache.getIfPresent("homeImage");
        if (homeImageCache!=null)
        {
            return new ResponseResult<>(HttpStatus.OK.value(),"查询轮播图成功",homeImageCache);
        }
        //Redis缓存
        RBucket<List<TbHomeImg>> homeImage = redissonClient.getBucket("homeImage",new KryoCodec());
        List<TbHomeImg> tbHomeImgs = homeImage.get();
        if (tbHomeImgs==null){
            //查询数据库
            List<TbHomeImg> homeImgAll = tbHomeImgService.getHomeImgAll();
            homeImage.set(homeImgAll,1, TimeUnit.HOURS);
            return new ResponseResult<>(HttpStatus.OK.value(),"查询轮播图成功",homeImgAll);
        }
        else
        {
            //写进本地缓存
            cache.put("homeImage",tbHomeImgs);
            return new ResponseResult<>(HttpStatus.OK.value(),"查询轮播图成功",tbHomeImgs);
        }

    }
}
